package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.ges.R;
import com.jxs.android.ges.data.VisitRecordInfo;

public class Bu_A_Case_Visit_Record_PageView extends LinearLayout {
	static public interface Visit_Record_Loading_Listener {
		public void vr_loadingData(int currentPage);
	}

	private Context context;

	private TextView null_has_data;
	private ListView listView;
	private int currentPage = 1;
	private VisitRecordAdapter adapter;

	private View loadMoreView;
	private Button loadMoreButton;
	private Visit_Record_Loading_Listener dataListener;

	public Bu_A_Case_Visit_Record_PageView(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	private void initView() {
		LayoutInflater.from(context).inflate(R.layout.bu_a_case_result_visit_records_item, this);
		null_has_data = (TextView) findViewById(R.id.null_has_data);
		loadMoreView = LayoutInflater.from(context).inflate(R.layout.bu_loading_btn, null);
		loadMoreButton = (Button) loadMoreView.findViewById(R.id.loadMoreButton);
		loadMoreButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				loadMoreButton.setText(R.string.prompt_loading); //
				if (null != dataListener) {
					dataListener.vr_loadingData(currentPage);
				}
			}
		});

		listView = (ListView) findViewById(R.id.common_listview);
		listView.addFooterView(loadMoreView);
		adapter = new VisitRecordAdapter();
		listView.setAdapter(adapter);

	}

	public void setDataListener(Visit_Record_Loading_Listener dataListener) {
		this.dataListener = dataListener;
	}

	public VisitRecordAdapter getAdapter() {
		return adapter;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setEmpty(String text) {
		null_has_data.setText(text);
		null_has_data.setVisibility(View.VISIBLE);
	}

	public void setEmptyVisibility(int visibility) {
		null_has_data.setVisibility(visibility);
	}

	public void setVisitRecordInfos(List<VisitRecordInfo> custInfos) {
		adapter.setVisitRecordInfos(custInfos);
		adapter.notifyDataSetChanged();
	}

	private class VisitRecordAdapter extends BaseAdapter {

		List<VisitRecordInfo> workNoteInfos = new ArrayList<VisitRecordInfo>();

		private ViewHolder holder = null;

		public void setVisitRecordInfos(List<VisitRecordInfo> custInfos) {
			if (currentPage == 1) {
				this.workNoteInfos.clear();
			}
			this.workNoteInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return workNoteInfos.size();
		}

		@Override
		public VisitRecordInfo getItem(int position) {
			return workNoteInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			VisitRecordInfo custInfo = workNoteInfos.get(position);

			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.bu_list_item_visit_recordes, null);
				holder = new ViewHolder();
				holder.index = (TextView) convertView.findViewById(R.id.index);
				holder.date = (TextView) convertView.findViewById(R.id.date);
				holder.person = (TextView) convertView.findViewById(R.id.person);
				holder.content = (TextView) convertView.findViewById(R.id.content);
				holder.manager = (TextView) convertView.findViewById(R.id.manager);
				holder.item_arrow = (ImageView) convertView.findViewById(R.id.item_arrow);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.index.setText("No." + String.valueOf(position + 1));
			holder.date.setText(Html.fromHtml("<font color='#990000'><b>" + custInfo.create_date + "</b></font> " + custInfo.week_desc));
			holder.person.setText(custInfo.cust_name);
			holder.manager.setText(custInfo.staff_name);

			String customHtml = (custInfo.basic + "\n" + custInfo.today_content).replace("\\n", "<br>");
			holder.content.setText(Html.fromHtml(customHtml));

			holder.item_arrow.setVisibility(View.GONE);
			return convertView;
		}

		class ViewHolder {
			TextView index, date, person, manager, content;
			ImageView item_arrow;
		}

	}

	public View getLoadMoreView() {
		return loadMoreView;
	}

	public Button getLoadMoreButton() {
		return loadMoreButton;
	}

}
